package com.albert.funny;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.text.format.DateUtils;
import android.widget.Toast;

public class LocationRecordManager extends AsyncTask<String, Void, Boolean>
{
    static final String directoryName = Environment.getExternalStorageDirectory() + "/gpsrecords";
    File file;
    Context context;
    public LocationRecordManager(Context context)
    {
        long timeStamp = System.currentTimeMillis();
        String dateString = DateUtils.formatDateTime(context, timeStamp, DateUtils.FORMAT_NUMERIC_DATE);
        File fileDir = new File(directoryName);
        if (!fileDir.exists())
        {
            boolean isSuccess = fileDir.mkdirs();
            System.out.println("isSuccess:" + isSuccess);
        }
        dateString = dateString.replace("/", "");
        file = new File(directoryName, dateString +".txt");
        this.context = context;
    }
    @Override
    protected Boolean doInBackground(String... params)
    {
        boolean isSuccess = false;
        if(!file.exists())
        {
            try
            {
                file.createNewFile();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (file.canWrite())
        {
            FileOutputStream fos = null;
            OutputStreamWriter osWriter = null;
            try
            {
                fos = new FileOutputStream(file, true);
                osWriter = new OutputStreamWriter(fos);
                for (int i = 0; i < params.length; i++)
                {
                    osWriter.append(params[i]);
                }
                osWriter.flush();
                isSuccess= true;
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(osWriter != null)
                {
                    try
                    {
                        osWriter.close();
                    } catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(fos != null)
                {
                    try
                    {
                        fos.close();
                    } catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
           
        }
        return Boolean.valueOf(isSuccess);
    }
    
    protected void onPostExecute (Boolean result)
    {
        if(result.booleanValue())
        {
            Toast.makeText(context, R.string.save_gps_success, 2000).show();
        }
        else
        {
            Toast.makeText(context, R.string.gpsWritefail, 2000).show();
        }
    }
}
